package com.yang.drawcard.book.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.core.click.IClick;
import com.core.utils.dbtutil.CheckUtil;
import com.yang.drawcard.R;
import com.yang.drawcard.book.domain.BookStc;
import com.yang.drawcard.video.domain.VideoStc;

import java.util.List;


/**
 * 项目名称：营销移动智能工作平台 </br>
 * 文件名：TermListAdapter.java</br>
 * 作者：hongen   </br>
 * 创建时间：2014-1-19</br>
 * 功能描述:终端列表adapter </br>
 * 版本 V 1.0</br>
 * 修改履历</br>
 * 日期      原因  BUG号    修改人 修改版本</br>
 */
public class BookListAdapter extends BaseAdapter {

    private final String TAG = "BookListAdapter";

    private static final RequestOptions BANNER_OPTIONS = new RequestOptions()
            // .diskCacheStrategy(DiskCacheStrategy.ALL)
            // .diskCacheStrategy(DiskCacheStrategy.NONE)
            // .diskCacheStrategy(DiskCacheStrategy.RESOURCE)// 将解码后的资源写入磁盘
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .dontAnimate()
            .centerCrop()
            .placeholder(R.color.white);

    private Activity context;
    private List<BookStc> dataLst;
    private IClick mListener;

    public BookListAdapter(Activity context, List<BookStc> termialLst, IClick listener) {
        this.context = context;
        this.dataLst = termialLst;
        this.mListener = listener;
    }

    @Override
    public int getCount() {
        if (CheckUtil.IsEmpty(this.dataLst)) {
            return 0;
        } else {
            return this.dataLst.size();
        }
    }

    @Override
    public Object getItem(int arg0) {
        if (CheckUtil.IsEmpty(this.dataLst)) {
            return null;
        } else {
            return this.dataLst.get(arg0);
        }
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_booklist_sdlv, null);
            holder.titleTv = (TextView) convertView.findViewById(R.id.item_booklist_tv_name);
            holder.picRl = (RelativeLayout) convertView.findViewById(R.id.item_booklist_rl_pic);//
            holder.picImg = (ImageView) convertView.findViewById(R.id.item_booklist_tv_pic);//
            holder.starringTv = (TextView) convertView.findViewById(R.id.item_booklist_tv_starring);//
            holder.mp4sizeTv = (TextView) convertView.findViewById(R.id.item_booklist_tv_size);//
            holder.dayTv = (TextView) convertView.findViewById(R.id.item_booklist_tv_day);//
            holder.lookTv = (TextView) convertView.findViewById(R.id.item_booklist_tv_look);//

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // 重置
        holder.titleTv.setText("");
        Glide.with(context)
                .load(R.color.white)
                .apply(BANNER_OPTIONS)
                .into(holder.picImg);
        holder.dayTv.setText("");

        BookStc item = dataLst.get(position);
        // 标题
        holder.titleTv.setText(item.getBookname());
        if (item.getBookname().length() > 25) {
            holder.titleTv.setTextSize(12);
        } else {
            holder.titleTv.setTextSize(14);
        }
        // 照片
        Glide.with(context)
                .load(item.getImageurl())
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))  // 设置圆角半径
                // .apply(BANNER_OPTIONS)
                .centerCrop()
                .into(holder.picImg);
        // 日期
        holder.dayTv.setText((position + 1) + "");


        holder.picRl.setOnClickListener(mListener);
        holder.picRl.setTag(position);
        holder.lookTv.setOnClickListener(mListener);
        holder.lookTv.setTag(position);

        return convertView;
    }

    private class ViewHolder {
        private TextView titleTv;
        private RelativeLayout picRl;
        private ImageView picImg;
        private TextView starringTv;
        private TextView mp4sizeTv;
        private TextView dayTv;
        private TextView lookTv;
    }
}
